/**
 * SysDictIndexAction.java
 * Created at 2017-1-24
 * Created by guhuajie
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.system.action;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.broadtext.common.bean.EntityPageBean;
import com.broadtext.common.exp.DaoException;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.rms.system.model.SysDictIndex;
import com.broadtext.rms.system.service.ISysDictIndexService;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

/**
 * <p>ClassName: SysDictIndexAction</p>
 * <p>Description: 字典索引表页面交互类</p>
 * <p>Author: guhuajie</p>
 * <p>Date: 2017-1-24</p>
 */
@Controller
@RequestMapping("/main/sysDictIndex")
public class SysDictIndexAction {
    /**
     * 日志
     */
    private static Logger logger = LoggerFactory.getLogger(SysDictIndexAction.class);  
    /**
     * sysDictIndexService 
     */
    @Autowired
    @Qualifier("sysDictIndexService")
    private ISysDictIndexService sysDictIndexService;
    
    /**
     * <p>
     * Description: 查询字典
     * </p>
     * 
     * @param request 请求对象
     * @param response 返回对象
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @RequestMapping(value = "/getDictIndex.mvc")
    @ResponseBody
    public Object getDict(HttpServletRequest request, HttpServletResponse response) {
        Map dataMap=new HashMap();
        try {
            PageBounds pageBounds;
            pageBounds = new PageBounds();
            Map<String, Object> map;
            String aoData;
            aoData = request.getParameter("aoData");
            JSONArray array;
            array = JSON.parseArray(aoData);
            map = new HashMap<String, Object>();
            if (array != null) {
                Iterator it;
                it = array.iterator();
                while (it.hasNext()) {
                    JSONObject obj;
                    obj = (JSONObject) it.next();
                    if(obj.get("value")!=null && !"".equals(obj.get("value")+"")){
                        map.put(obj.get("name")+"", obj.get("value"));
                    }
                }
            }
            pageBounds.setLimit(Integer.parseInt(map.get("iDisplayLength").toString()));
            pageBounds.setPage(Integer.parseInt(map.get("iDisplayStart").toString()) / pageBounds.getLimit()+1);
            EntityPageBean<SysDictIndex> pageBean = this.sysDictIndexService.findEntityPageByQuery(map, pageBounds);
            dataMap.put("recordsTotal", pageBean.getiTotalRecords());
            dataMap.put("recordsFiltered", pageBean.getiTotalRecords());
            dataMap.put("aaData", pageBean.getResults());
            
        } catch (ServiceException e) {
            logger.error("字典查询出错：", e);
            e.printStackTrace();
        }
        return dataMap;
    }
    
    /**
     * <p>Description: 保存 或 更新字典索引</p>
     * @param dict 字典对象
     * @return String 操作结果
     */
    @RequestMapping(value = "/saveOrUpdateDictIndex.mvc")
    @ResponseBody
    public Object saveOrUpdateDict(SysDictIndex dict, HttpServletRequest request) {
        Map<String, Object> dataMap;
        dataMap = new HashMap<String, Object>();
        try {
            //当前登录人
            String currentUser;
            currentUser = String.valueOf(request.getSession().getAttribute("sys.userName"));
            dataMap.put("msg", this.sysDictIndexService.saveOrUpdateDict(currentUser, dict));
        } catch (ServiceException | DaoException e) {
            dataMap.put("msg", "FAILURE");
            logger.error("新增字典出错：", e);
            e.printStackTrace();
        }
        return dataMap;
    }
    
    /**
     * 
     * <p>
     * Description: 根据id获取编辑对象
     * </p>
     * 
     * @param id 主键id
     * @return 对象
     * @throws DaoException 异常
     * @throws ServiceException 异常
     */
    @RequestMapping(value = "/getDictById.mvc")
    @ResponseBody
    public SysDictIndex getDictById(@RequestParam("pkId") String id) throws DaoException, ServiceException {
        return this.sysDictIndexService.findEntityById(id);
    }
    
    /**
     * 
     * <p>Description: 删除字典索引</p>
     * @param request 请求
     * @param pkId 主键id
     * @throws DaoException 异常
     * @throws ServiceException 异常
     */
    @RequestMapping(value = "/delDictIndex.mvc")
    @ResponseBody
    public Object delDictIndex(HttpServletRequest request, String pkId) throws DaoException, ServiceException {
        Map<String, Object> map;
        map = new HashMap<String, Object>();
        try {
            String[] idArray;
            idArray = pkId.split(",");
            String updateUser;
            updateUser = String.valueOf(request.getSession().getAttribute("sys.userName"));
            this.sysDictIndexService.deleteDictIndex(idArray, updateUser);
            map.put("msg", "SUCCESS");
        } catch (Exception e) {
            map.put("msg", "FAILURE");
            logger.error("删除字典索引出错：", e);
            e.printStackTrace();
        }
        return map;
    }
    
    /**
     * 
     * <p>Description: 根据字典key获取字典</p>
     * @param parentKey 字典key
     * @return 字典详情
     * @throws DaoException 异常
     */
    @RequestMapping(value = "/getDictByParentKey.mvc")
    @ResponseBody
    public Map<String, Object> getDictByParentKey(String parentKey) throws DaoException{
        Map<String, Object> map;
        map = new HashMap<String, Object>();
        List<Object> list;
        list = this.sysDictIndexService.getDictByParentKey(parentKey);
        map.put("dict", list);
        return map;
    }
}
